package com.sskj.finance.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.finance.R;
import com.sskj.finance.R2;
import com.sskj.finance.bean.HomeQbwAccountList;
import com.sskj.finance.presenter.TransferActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.SchemeUtil;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.FINAN_TRANSFER)//划转
public class TransferActivity extends BaseActivity<TransferActivityPresenter> {
    @BindView(R2.id.tvTransferType)
    TextView tvTransferType;
    @BindView(R2.id.tvCoin)
    TextView tvCoin;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.tvCoinType)
    TextView tvCoinType;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private String type = "1";//1交易账户to理财账户 2理财账户to交易账户
    private BottomSheetDialog bottomSheetTransferType;
    private HomeQbwAccountList homeQbwAccountList;
    private BottomSheetDialog bottomSheetCoinType;
    private Disposable disposable;
    private List<HomeQbwAccountList.LcBean> coinList;
    private HomeQbwAccountList.LcBean coinBean;
    private String code;
    private Disposable subscribeLc;
    private Disposable subscribeZc;

    @Override
    protected int getLayoutId() {
        return R.layout.finan_activity_transfer;
    }

    @Override
    public TransferActivityPresenter getPresenter() {
        return new TransferActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.finan_transferActivity1_j));
        setRightTitle(App.INSTANCE.getString(R.string.finan_transferRecordActivity1_j), v -> {
            ARouter.getInstance().build(RConfig.FINAN_TRANSFER_RECORD).navigation();
        });
        ClickUtil.click(tvTransferType, () -> {
            showTransferType();
        });
        ClickUtil.click(tvCoin, () -> {
            showCoin();
        });
        ClickUtil.click(btSubmit, () -> {

            AndroidNextInputs inputs = new AndroidNextInputs();
            inputs.add(etNum, SchemeUtil.getEmptyScheme(App.INSTANCE.getString(R.string.finan_transferActivity3_j)))
                    .add(etPwd, SchemeUtil.getEmptyScheme(App.INSTANCE.getString(R.string.finan_loginRequest5_j)));
            if (inputs.test()) {
                mPresenter.transfer(type, etNum.getText().toString(), etPwd.getText().toString(), code);
            }
        });
    }

    private void showCoin() {
        if (bottomSheetCoinType == null) {
            mPresenter.homeQbwAccountList();
        }
        bottomSheetCoinType.show();
    }

    @Override
    protected void initData() {
        mPresenter.homeQbwAccountList();
    }

    private void changeCoinType() {
        coinList = type.equals("1") ? homeQbwAccountList.getZc() : homeQbwAccountList.getLc();
        if (coinList != null && coinList.size() > 0) {
            coinBean = coinList.get(0);
            tvCoin.setText(coinBean.getMark());
            code = coinBean.getMark();
            etNum.setHint(App.INSTANCE.getString(R.string.finan_transferActivity5_j) + CoinUtil.keepCoinNumMax(code,coinBean.getUsable()));
            tvCoinType.setText(coinBean.getMark());
        }
        disposable = Flowable.fromIterable(coinList)
                .map(HomeQbwAccountList.LcBean::getMark)
                .toList()
                .subscribe((strings, throwable) -> {
                    bottomSheetCoinType = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, view) -> {
                        bottomSheetCoinType.dismiss();
                        TextView textView = view.findViewById(R.id.tv_content);
                        tvCoin.setText(textView.getText());
                        coinBean = coinList.get(position);
                        code = coinBean.getMark();
                        etNum.setHint(App.INSTANCE.getString(R.string.finan_transferActivity5_j) + coinBean.getUsable());
                        tvCoinType.setText(coinBean.getMark());
                    }, strings);
                });

    }

    private void showTransferType() {
        if (bottomSheetTransferType == null) {
            bottomSheetTransferType = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, view) -> {
                bottomSheetTransferType.dismiss();
                TextView textView = view.findViewById(R.id.tv_content);

                tvTransferType.setText(textView.getText());
                type = position + 1 + "";
                changeCoinType();

            }, App.INSTANCE.getString(R.string.finan_transferActivity6_j), App.INSTANCE.getString(R.string.finan_transferActivity7_j));
        }
        bottomSheetTransferType.show();
    }


    public void updateCoin(HomeQbwAccountList data) {
        subscribeLc = Flowable.fromIterable(data.getLc())
                .filter(lcBean -> lcBean.getIs_transfer() == 1)
                .toList()
                .subscribe((lcBeans, throwable) -> {
                    data.setLc(lcBeans);
                });
        subscribeZc = Flowable.fromIterable(data.getZc())
                .filter(lcBean -> lcBean.getIs_transfer() == 1)
                .toList()
                .subscribe((lcBeans, throwable) -> {
                    data.setZc(lcBeans);
                });
        homeQbwAccountList = data;
        changeCoinType();

    }

    public void transferSuccess() {
        ARouter.getInstance().build(RConfig.FINAN_TRANSFER_RECORD).navigation();
        finish();
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(subscribeLc);
        DisposUtil.close(subscribeZc);
        super.onDestroy();
    }
}
